var Chienlv = {
    addCat: function() {
        if ($(".choseModel:checked").length > 0)
        {
            parent.$("#Article_primary_category").val($(".choseModel:checked").val());
            parent.$("#Article_primary_category_name").val($(".choseModel:checked").attr("rel"));
        }
        parent.$.modal.close();
    },
    loadPage: function(url, id) {
        $('.grid-view').addClass('loading');
        $.ajax({
            type: "GET",
            url: basePath + url,
            data: "ajax=1",
            success: function(data) {
                $('#' + id).html(data);
                $('.grid-view').removeClass('loading');
            }
        })
    },
    deleteAll: function(frmId, url) {
        $("#" + frmId).attr('action', basePath + url);
        if (!$("input[name='select-row[]']:checked").attr('checked')) {
            alert(LANG.DELETE_CHECKBOX_EMPTY);
            return;
        }
        if (confirm(LANG.DELETE_ALL_CONFIRM)) {
            $("#" + frmId).submit();
        }
        return;
    },
    numbersOnly: function(ob){ob.val(ob.val().replace(/[^0-9]/g, ''));},
    hideMessages: function(element){$(element).parent("#message").fadeOut();},
    onKeyPress: function(field, event)
    {
        if (event.keyCode == 13) {
            for (i = 0; i < field.form.elements.length; i++)
                if (field.form.elements[i].tabIndex == field.tabIndex + 1) {
                    field.form.elements[i].focus();
                    if (field.form.elements[i].type == "text")
                        field.form.elements[i].select();
                    break;
                }
            return false;
        }
        return true;
    },
    autoRun: function() {
        $('#common-tootip').live('click', function() {
            var $obj = $(this);
            ajax_showTooltip(window.event, basePath + '/' + $obj.attr('rev') + '/detail?id=' + $obj.attr('rel') + '&YII_CSRF_TOKEN=' + YII_CSRF_TOKEN, this);
        });
        $("#mainmenu ul li").click(function() { //When trigger is clicked...
            //Following events are applied to the subnav itself (moving subnav up and down)
            $(this).parent().find("#mainmenu ul li ul").slideDown('fast').show(); //Drop down the subnav on click
            $(this).parent().hover(function() {
            }, function() {
                $(this).parent().find("#mainmenu ul li ul").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
            });
            //Following events are applied to the trigger (Hover events for the trigger)
        }).hover(function() {
            $(this).addClass("subhover"); //On hover over, add class "subhover"
        }, function() {	//On Hover Out
            $(this).removeClass("subhover"); //On hover out, remove class "subhover"
        });

        //Display / Hide category select box
        $('input[name="delOption"]').click(function() {
            if (this.value == '1') {
                $('#changedId').attr('style', 'display: none');
            } else {
                $('#changedId').attr('style', 'display: inline');
            }
        });

        //Check all
        $('#select-all').live('click', function() {
            $("INPUT[type='checkbox']").attr('checked', $('#select-all').is(':checked'));
            if (this.checked)
                $(".items tr.odd, .items tr.even").addClass('selected');
            else
                $(".items tr.odd, .items tr.even").removeClass('selected');
        });

        $(".items tr.odd, .items tr.even").live('click', function() {
            if ($('#is-select-all-item').length > 0)
                $('#is-select-all-item').val(0);
            if ($('tr#select-all-record').length > 0)
                $('tr#select-all-record').remove();
        });

        $(".numbersOnly").live('keyup paste', function(e) {
            var ob = $(this);
            if (e.type == 'paste') {
                setTimeout(function() {
                    Chienlv.numbersOnly(ob)
                }, 1);
            } else
                Chienlv.numbersOnly(ob);
        });

        $('.floatOnly').keyup(function() {
            this.value = this.value.replace(/[^0-9\.]/g, '');
        });

        $('.items .icon-status').live('click', function() {
            mName = $('#moduleName').val();
            $('.grid-view').addClass('loading');
            img = this;
            $.ajax({
                type: 'POST',
                url: basePath + '/' + mName + '/changeStatus',
                data: {
                    id: img.name,
                    ajax: true,
                    YII_CSRF_TOKEN: YII_CSRF_TOKEN
                },
                success: function() {
                    if (img.alt == 'enabled') {
                        img.alt = 'disabled';
                        img.src = baseUrl + '/css/admin/images/publish_x.png';
                    } else {
                        img.alt = 'enabled';
                        img.src = baseUrl + '/css/admin/images/tick.png';
                    }
                    $('.grid-view').removeClass('loading');
                }
            });
        });

        $('.items .icon-hot').live('click', function() {
            mName = $('#moduleName').val();
            $('.grid-view').addClass('loading');
            img = this;
            $.ajax({
                type: 'POST',
                url: basePath + '/' + mName + '/changeHotStatus',
                data: {
                    id: img.name,
                    ajax: true,
                    YII_CSRF_TOKEN: YII_CSRF_TOKEN
                },
                success: function() {
                    if (img.alt == 'enabled') {
                        img.alt = 'disabled';
                        img.src = baseUrl + '/css/admin/images/publish_x.png';
                    } else {
                        img.alt = 'enabled';
                        img.src = baseUrl + '/css/admin/images/tick.png';
                    }
                    $('.grid-view').removeClass('loading');
                }
            });
        });
        $(".messages").delay(1000).slideUp(1000);
        $(".warnings").delay(1000).slideUp(1000);
        $("#btnCloseMessage").click(function(){Chienlv.hideMessages(this);})
        /* using for rewrite url for site */
        $('input[rel=name]').blur(function() {
            var $outPut = $(this).attr('value');
            var $url = $('input[rel=url_key]');
            var $title = $('textarea[rel=title]');
            var $keywords = $('textarea[rel=keywords]');
            var $description = $('textarea[rel=description]');
            $.makeFriendlyUrl($outPut, $url);
            $title.val($outPut);
            $keywords.val($outPut);
            $description.val($outPut);
        });
        $.makeFriendlyUrl = function($name, obj)
        {
            $.ajax({type: "post",url: basePath + '/site/makeFriendlyUrl',data: {YII_CSRF_TOKEN: YII_CSRF_TOKEN, name: $name},success: function(result){$(obj).val(result);}});
        }
        $('input[rev=common-check-name]').blur(function() {
            var $name = this.value;
            var $modul = $('#model-name').val();
            var $span = $('#load-img-check');
            if ($name.length > 0)
            {
                $span.html('<img src="' + baseUrl + '/css/admin/images/loading.gif" border="0" />');
                $.ajax({
                    type: "post",
                    url: basePath + '/' + $modul + '/checkName',
                    data: {YII_CSRF_TOKEN: YII_CSRF_TOKEN, name: $name},
                    success: function(responseText) {
                        var result = $.parseJSON(responseText);
                        if (result.success)
                        {
                            $span.html(result.link);
                        } else {
                            $span.html('');
                        }
                    }
                });
            }
        });
    }
};
Chienlv.autoRun();